package com.xt.back.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xt.back.constant.ResultDTO;
import com.xt.back.entity.User;
import com.xt.back.service.IUserService;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 * 用户信息表 前端控制器
 * </p>
 *
 * @author leotong
 * @since 2022-04-03
 */
@RestController
@RequestMapping("/back/user")
@AllArgsConstructor
@CrossOrigin
public class UserController {

    private final IUserService userService;

    @ApiOperation("管理员登陆")
    @PostMapping("/login")
    public ResultDTO<String> login(@RequestBody User user){
        return ResultDTO.success(userService.login(user));
    }

    @ApiOperation("添加用户")
    @PostMapping("/add")
    public ResultDTO<Boolean> addUser(@RequestBody User user){
        return ResultDTO.success(userService.save(user));
    }

    @ApiOperation("删除用户")
    @GetMapping("/remove")
    public ResultDTO<Boolean> deleteUser(int id){
        return ResultDTO.success(userService.removeById(id));
    }

    @ApiOperation("更新用户")
    @PostMapping("/update")
    public ResultDTO<Boolean> updateUser(@RequestBody User user){
        return ResultDTO.success(userService.updateById(user));
    }

    @ApiOperation("分页查询用户")
    @PostMapping("/getUserByPage")
    public ResultDTO<Page<User>> getUserByPage(@RequestBody User user){
        return ResultDTO.success(userService.getUserByPage(user));
    }

    @ApiOperation("查询用户")
    @GetMapping("/getAllUser")
    public ResultDTO<List<User>> getAllUser(){
        return ResultDTO.success(userService.list());
    }
}
